/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apt.Bean.MemberEnties;
import org.apt.connectDB.MahoaPassword;
import org.apt.connectDB.MyConnect;

/**
 *
 * @author Administrator
 */
public class MemberDao {

    /**
     * Fuction Get a Member by user name input
     * @param username
     * @return a member
     */
    public static MemberEnties getMemberByUserName(String username) {
        MemberEnties member = new MemberEnties();
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            String sql = "select * FROM Member where UserName='" + username + "' ";
            con = MyConnect.getConnection();
            st = con.createStatement();
            rs = st.executeQuery(sql);

            if (rs.next()) {
                member.setTxtUserName(rs.getString("UserName"));
                member.setTxtPassword(rs.getString("Password"));
                member.setTxtFullName(rs.getString("FullName"));
                member.setTxtImage(rs.getString("Image"));
                member.setTxtEmail(rs.getString("Email"));
                member.setTxtBirthday(rs.getString("DOB"));
                member.setTxtIDCard(rs.getString("IDCard"));
                member.setTxtMobile(rs.getString("MobilePhone"));
                member.setTxtHomePhone(rs.getString("HomePhone"));
                member.setTxtAddress(rs.getString("Address"));
                member.setTxtStatus(rs.getBoolean("Status"));
                member.setTxtDescription(rs.getString("Description"));
                member.setTxtgender(rs.getBoolean("Gender"));
            } else {
                return null;
            }
        } catch (SQLException ex) {
            Logger.getLogger(MemberDao.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        } finally {
            MyConnect.closeConntion(con, st, rs);
        }
        return member;
    }

    /**
     * Function Edit Member
     * @param member object
     * @return true if success or fail
     */
    public static boolean EditMemberB(MemberEnties mber) {
        boolean resuilt = false;
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            if (mber != null) {
                String username = mber.getTxtUserName();
                if (checkExistUser(username)) {
                    String sql = "update Member  set FullName='" + mber.getTxtFullName() + "', Image='" + mber.getTxtImage() + "', "
                            + "Email='" + mber.getTxtEmail() + "', DOB='" + mber.getTxtBirthday() + "', "
                            + "IDCard='" + mber.getTxtIDCard() + "',MobilePhone='" + mber.getTxtMobile() + "',"
                            + "HomePhone='" + mber.getTxtHomePhone() + "',Address='" + mber.getTxtAddress() + "',"
                            + "Description= '',Gender='" + mber.isTxtgender() + "' "
                            + "  where UserName='" + username + "' ";
                    con = MyConnect.getConnection();
                    st = con.createStatement();
                    int resuilt2 = st.executeUpdate(sql);
                    if (resuilt2 > 0) {
                        resuilt = true;
                    } else {
                        resuilt = true;
                    }
                } else {
                    resuilt = false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnect.closeConntion(con, st, rs);
        }
        return resuilt;
    }

    /**
     * Function change password
     * @param member object
     * @return true if success or fail
     */
    public static boolean ChangePassWordUser(MemberEnties mber) {
        boolean resuilt = false;
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            if (mber != null) {
                String username = mber.getTxtUserName();
                String password = MahoaPassword.MD5Password(mber.getTxtPassword());
                if (checkExistUser(username)) {
                    String sql = "update Member  set Password='" + password + "' where UserName='" + username + "' ";
                    con = MyConnect.getConnection();
                    st = con.createStatement();
                    int resuilt2 = st.executeUpdate(sql);
                    if (resuilt2 > 0) {
                        resuilt = true;
                    } else {
                        resuilt = false;
                    }
                } else {
                    resuilt = false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnect.closeConntion(con, st, rs);
        }
        return resuilt;
    }

    /**
     * Function RecoveryPassword
     * @param member object
     * @return true if success or fail
     */
    public static boolean RecoveryPassword(MemberEnties mber) {
        boolean resuilt = false;
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            if (mber != null) {
                String username = mber.getTxtUserName();
                String email = mber.getTxtEmail();
                String password = MahoaPassword.MD5Password(mber.getTxtPassword());
                String sql = " update Member  set Password='" + password + "' where UserName='" + username + "' and Email='" + email + "' ";
                con = MyConnect.getConnection();
                st = con.createStatement();
                int resuilt2 = st.executeUpdate(sql);
                if (resuilt2 > 0) {
                    resuilt = true;
                } else {
                    resuilt = false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnect.closeConntion(con, st, rs);
        }
        return resuilt;
    }

    /**
     * Functin check exist user name
     * @param user
     * @return true if success or fail
     */
    public static boolean checkExistUser(String username) {
        boolean resuilt = false;
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            con = MyConnect.getConnection();
            st = con.createStatement();
            rs = st.executeQuery("select * from Member where UserName='" + username + "'");
            if (rs.next()) {
                resuilt = true;
            } else {
                resuilt = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnect.closeConntion(con, st, rs);
        }
        return resuilt;
    }

    /**
     * Functin check exist user name
     * @param user
     * @return true if success or fail
     */
    public static boolean checkExistUserByEmail(String username, String email) {
        boolean resuilt = false;
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            con = MyConnect.getConnection();
            st = con.createStatement();
            rs = st.executeQuery("select * from Member where UserName='" + username + "' and Email='" + email + "'");
            if (rs.next()) {
                resuilt = true;
            } else {
                resuilt = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Erro:" + e.getMessage());
        } finally {
            MyConnect.closeConntion(con, st, rs);
        }
        return resuilt;
    }

    /**
     * Check exist email member
     * @param email
     * @return true if success else fail
     */
    public static boolean checkExistUser(String username, String password) {
        boolean resuilt = false;
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            con = MyConnect.getConnection();
            st = con.createStatement();
            rs = st.executeQuery("select * from Member where UserName='" + username + "' and Password='" + MahoaPassword.MD5Password(password) + "'");
            if (rs.next()) {
                resuilt = true;
            } else {
                resuilt = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyConnect.closeConntion(con, st, rs);
        }
        return resuilt;
    }

    public static void main(String[] args) {
        MemberEnties m = new MemberEnties();
        m.setTxtUserName("vinh");
        m.setTxtEmail("nguyenanhvinh.it@gmail.com");
        m.setTxtPassword("vinhnguyen");
        boolean a = RecoveryPassword(m);
        System.out.println(a);
    }
}
